package com.fs.book.job;

// 导入相关的包
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DataBackUpTask {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 每隔10秒执行一次备份任务
    @Scheduled(cron = "0/10 * * * * ?")
    public void backupDatabase() {
        try {
//            createBeiFenTable();
//            BenFenData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 创建备份表
    private void createBackupTable() {
        String createBackupTableSql = "create table if not exists tb_userback as select * from tb_userinfo where 1 = 2";
        jdbcTemplate.execute(createBackupTableSql);
    }

    // 全量备份数据
    private void backupData() {
        String backupDataSql = "insert into tb_userback select * from tb_userinfo";
        jdbcTemplate.execute(backupDataSql);
    }
}